package com.koron.css2.projectCenter.impl;

import com.koron.css2.ImportExcelInterface;
import com.koron.css2.baseConfig.mapper.BaseBookMapper;
import com.koron.css2.baseConfig.mapper.TradeClassifyMapper;
import com.koron.css2.baseConfig.mapper.WaterTypeMapper;
import com.koron.css2.baseConfig.queryBean.BaseBookQueryBean;
import com.koron.css2.baseConfig.queryBean.WaterTypeQueryBean;
import com.koron.css2.baseConfig.vo.BaseBookListVO;
import com.koron.css2.baseConfig.vo.SelectVO;
import com.koron.css2.baseConfig.vo.WaterTypeVO;
import com.koron.css2.common.dto.HeaderInfoDto;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.vo.DictionaryVO;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PjConsultMsImport  implements ImportExcelInterface{

	@Override
	public MessageBean<?> importExel(SessionFactory factory, UserInfoBean userInfo, RequestBean req,
			List<Map<String, String>> data,MultipartFile file) {
		@SuppressWarnings("rawtypes")
		MessageBean<List> message = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "导入成功", List.class);
		if (data == null || data.size() == 0) {
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "获取文件数据失败，请检查列名是否正确。", null);
		}
		TradeClassifyMapper tradeClassifyMapper = factory.getMapper(TradeClassifyMapper.class,"_default");
		BaseBookMapper baseBookMapper = factory.getMapper(BaseBookMapper.class);
		WaterTypeMapper mapper = factory.getMapper(WaterTypeMapper.class);
		List<SelectVO> TradeClassifyList = tradeClassifyMapper.tradeClassifySelect();
		List<BaseBookListVO> baseBooklist = baseBookMapper.select(new BaseBookQueryBean());
		
		
		
		List<DictionaryVO> MMT = Tools.listDicByCode(factory, "MMT");//水表类型
		List<DictionaryVO> uws = Tools.listDicByCode(factory, "UWS");//用水对象
		List<DictionaryVO> cmc = Tools.listDicByCode(factory, "CMC");//水表用途
		List<DictionaryVO> ctl = Tools.listDicByCode(factory, "CTL");//装表位置
		List<DictionaryVO> rtp = Tools.listDicByCode(factory, "RTP");//抄表周期
		
		for(Map<String,String> map: data) {		
			if(StringUtils.isNotEmpty(map.get("tradeClassify"))) {		//行业分类
				for(SelectVO tradeClassify:TradeClassifyList) {
					if(tradeClassify.getName().equals(map.get("tradeClassify")))
						map.replace("tradeClassify", tradeClassify.getId());
				}
			}
			
			if(StringUtils.isNotEmpty(map.get("meterType"))) {		//水表类型
				for(DictionaryVO meterType:MMT) {
					if(meterType.getName().equals(map.get("meterType")))
						map.replace("meterType", meterType.getValue());
				}
			}
			
			if(StringUtils.isNotEmpty(map.get("useWaterObject"))) {		//用水对象
				for(DictionaryVO useWaterObject:uws) {
					if(useWaterObject.getName().equals(map.get("useWaterObject")))
						map.replace("useWaterObject", useWaterObject.getValue());
				}
			}
			
			if(StringUtils.isNotEmpty(map.get("meterUse"))) {		//水表用途
				for(DictionaryVO meterUse:cmc) {
					if(meterUse.getName().equals(map.get("meterUse")))
						map.replace("meterUse", meterUse.getValue());
				}
			}
			
			if(StringUtils.isNotEmpty(map.get("setupMeterLocation"))) {		//装表位置
				for(DictionaryVO setupMeterLocation:ctl) {
					if(setupMeterLocation.getName().equals(map.get("setupMeterLocation")))
						map.replace("setupMeterLocation", setupMeterLocation.getValue());
				}
			}
			
			if(StringUtils.isNotEmpty(map.get("bookNo"))) {		//册本号
				int isExits = 0;
				for(BaseBookListVO baseBook:baseBooklist) {
					if(baseBook.getBookNo().equals(map.get("bookNo")))
						isExits = 1;
				}
				if(isExits == 0)
					return MessageBean.create(Constant.MESSAGE_INT_FAIL, "册本号不存在,"+map.get("bookNo"), null);
			}
			
			if(StringUtils.isNotEmpty(map.get("meterReadingCycle"))) {		//抄表周期
				for(DictionaryVO meterReadingCycle:rtp) {
					if(meterReadingCycle.getName().equals(map.get("meterReadingCycle")))
						map.replace("meterReadingCycle", meterReadingCycle.getValue());
				}
			}
			
			if(StringUtils.isNotEmpty(map.get("useWaterType"))) {		//用水类型
				WaterTypeQueryBean selectBean = new WaterTypeQueryBean();
				selectBean.setStatus(1);//只查询启用的
		        selectBean.setWaterTypeName(map.get("useWaterType"));
		        List<WaterTypeVO> list = mapper.findWaterType(selectBean);
		        if(list!=null && list.size()>0) {
		        	map.replace("useWaterType", list.get(0).getId());
		        }
			}
			
			if(StringUtils.isNotEmpty(map.get("inflate"))) {	//是否加压
				if("是".equals(map.get("inflate")))
					map.replace("inflate", "1");
				else
					map.replace("inflate", "0");
			}
			
			if(StringUtils.isNotEmpty(map.get("inflate"))) {	//是否二供
				if("是".equals(map.get("secondaryWater")))
					map.replace("secondaryWater", "1");
				else
					map.replace("secondaryWater", "0");
			}
			
		}
		message.setData(data);
		return message;
	}

	@Override
	public List<HeaderInfoDto> getHeaderInfo(UserInfoBean userInfo) {
		List<HeaderInfoDto> headerInfos = new ArrayList<HeaderInfoDto>();
		headerInfos.add(new HeaderInfoDto("房产证号", true, "houseCert"));
		headerInfos.add(new HeaderInfoDto("供水合同编号", true, "contractNo"));
		headerInfos.add(new HeaderInfoDto("供水合同签订地址", true, "contractAddr"));
		headerInfos.add(new HeaderInfoDto("合同签订日期", true, "contractDate"));
		headerInfos.add(new HeaderInfoDto("开户日期", true, "openDate"));
		headerInfos.add(new HeaderInfoDto("行业分类", true, "tradeClassify"));
		headerInfos.add(new HeaderInfoDto("水表类型", true, "meterType"));
		headerInfos.add(new HeaderInfoDto("用水对象", true, "useWaterObject"));
		headerInfos.add(new HeaderInfoDto("水表用途", true, "meterUse"));
		headerInfos.add(new HeaderInfoDto("总表编号", true, "fatherMeterNo"));
		headerInfos.add(new HeaderInfoDto("水表编号", true, "meterNo"));
		headerInfos.add(new HeaderInfoDto("装表地址", true, "setupMeterAddr"));
		headerInfos.add(new HeaderInfoDto("装表位置", true, "setupMeterLocation"));
		headerInfos.add(new HeaderInfoDto("装表日期", true, "setupMeterDate"));
		headerInfos.add(new HeaderInfoDto("册本号", true, "bookNo"));
		headerInfos.add(new HeaderInfoDto("排序号", true, "sortNo"));
		headerInfos.add(new HeaderInfoDto("抄表周期", true, "meterReadingCycle"));
		headerInfos.add(new HeaderInfoDto("用水类型", true, "useWaterType"));
		headerInfos.add(new HeaderInfoDto("是否加压", true, "inflate"));
		headerInfos.add(new HeaderInfoDto("是否二供", true, "secondaryWater"));
		return headerInfos;
	}

	@Override
	public boolean isCheckHeader() {
		// TODO Auto-generated method stub
		return false;
	}

}
